@{
    ViewData["Title"] = "添加外协入库单";
    Layout = null;
}

<link href="~/layui/css/layui.css" rel="stylesheet" />
<script src="~/layui/layui.js"></script>

<style>
    body {
        padding: 20px;
    }
    .layui-form-label {
        width: 120px;
    }
    .layui-input-block {
        margin-left: 150px;
    }
</style>

<form class="layui-form" lay-filter="outsourcingAddForm">
    <input type="hidden" name="id" id="id" value="0">
    <div class="layui-form-item">
        <label class="layui-form-label">
            <span style="color:red;">*</span> 入库单编号
        </label>
        <div class="layui-input-block">
            <input type="text" name="inboundCode" placeholder="请输入入库单编号" class="layui-input" required lay-verify="required">
            <input type="checkbox" name="autoGenerate" title="自动生成" lay-filter="autoGenerate" value="1">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            <span style="color:red;">*</span> 入库单名称
        </label>
        <div class="layui-input-block">
            <input type="text" name="inboundName" placeholder="请输入入库单名称" class="layui-input" required lay-verify="required">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            <span style="color:red;">*</span> 入库日期
        </label>
        <div class="layui-input-block">
            <input type="text" name="inboundDate" id="inboundDate" placeholder="请选择入库日期" class="layui-input" required lay-verify="required">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            <span style="color:red;">*</span> 外协工单
        </label>
        <div class="layui-input-block">
            <input type="text" name="workOrders" placeholder="请输入外协工单" class="layui-input" required lay-verify="required">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            单据状态
        </label>
        <div class="layui-input-block">
            <input type="text" name="documentStatus" value="草稿" class="layui-input" readonly>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            采购订单号
        </label>
        <div class="layui-input-block">
            <input type="text" name="purchaseOrderNumber" placeholder="请输入采购订单号" class="layui-input">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            <span style="color:red;">*</span> 供应商
        </label>
        <div class="layui-input-block">
            <select name="vendor" lay-filter="vendorFilter" required lay-verify="required">
                <option value="">请选择</option>
            </select>
            <input type="hidden" name="vendorCode" id="vendorCode">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">
            入库仓库
        </label>
        <div class="layui-input-block">
            <select name="warehouseId" lay-filter="warehouseFilter">
                <option value="">请选择</option>
            </select>
        </div>
    </div>
    
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">
            备注
        </label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入备注信息" class="layui-textarea"></textarea>
        </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="outsourcingAddSubmit">保存</button>
            <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
        </div>
    </div>
</form>

<script>
    layui.use(['form', 'layer', 'laydate'], function () {
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.$;
        
        // 获取URL参数
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        
        // 日期选择器
        laydate.render({
            elem: '#inboundDate',
            value: new Date()
        });
        
        // 自动生成入库单编号
        form.on('checkbox(autoGenerate)', function(data) {
            if (data.elem.checked) {
                // 根据时间生成编码 格式：OS + 年月日时分秒
                var now = new Date();
                var year = now.getFullYear();
                var month = (now.getMonth() + 1).toString().padStart(2, '0');
                var day = now.getDate().toString().padStart(2, '0');
                var hours = now.getHours().toString().padStart(2, '0');
                var minutes = now.getMinutes().toString().padStart(2, '0');
                var seconds = now.getSeconds().toString().padStart(2, '0');
                var generatedCode = 'OS' + year + month + day + hours + minutes + seconds;
                $('input[name="inboundCode"]').val(generatedCode).attr('readonly', true);
            } else {
                $('input[name="inboundCode"]').val('').removeAttr('readonly');
            }
        });
        
        // 监听供应商选择事件
        form.on('select(vendorFilter)', function(data) {
            var code = $(data.elem).find("option:selected").attr("data-code");
            $('#vendorCode').val(code || '');
        });
        
        // 表单提交
        form.on('submit(outsourcingAddSubmit)', function(data) {
            // 设置单据状态为数字值
            data.field.documentStatus = 1; // 1表示草稿
            // 添加materielCode字段，默认值为空
            data.field.materielCode = '';
            
            var url = '/Outsourcing/AddProcessConfiguration';
            // 如果是修改操作，则改变请求地址
            if (data.field.id && data.field.id > 0) {
                url = '/Outsourcing/UpdateProcessConfiguration';
            }
            
            // 提交表单
            $.ajax({
                url: url,
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function(res) {
                    if (res.success) {
                        layer.msg(data.field.id > 0 ? '修改成功' : '添加成功', {icon: 1, time: 1000}, function() {
                            // 关闭当前弹出层
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(data.field.id > 0 ? '修改失败: ' : '添加失败: ' + res.message, {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('服务器错误，请稍后再试', {icon: 2});
                }
            });
            return false;
        });
        
        // 取消按钮
        $('#cancelBtn').on('click', function() {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        });
        
        // 加载仓库下拉选择
        function loadWarehouses() {
            var warehouses = [
                { id: 1, warehouseName: '原料仓库' },
                { id: 2, warehouseName: '成品仓库' },
                { id: 3, warehouseName: '半成品仓库' }
            ];
            
            var html = '<option value="">请选择</option>';
            $.each(warehouses, function(i, item) {
                html += '<option value="' + item.id + '">' + item.warehouseName + '</option>';
            });
            $('select[name="warehouseId"]').html(html);
            form.render('select');
        }
        
        // 加载供应商下拉选择
        function loadVendors() {
            var vendors = [
                { vendorCode: 'V001', vendorName: '上海机械制造有限公司' },
                { vendorCode: 'V002', vendorName: '北京精密零件厂' },
                { vendorCode: 'V003', vendorName: '深圳电子科技有限公司' }
            ];
            
            var html = '<option value="">请选择</option>';
            $.each(vendors, function(i, item) {
                html += '<option value="' + item.vendorName + '" data-code="' + item.vendorCode + '">' + item.vendorName + '</option>';
            });
            $('select[name="vendor"]').html(html);
            form.render('select');
        }
        
        // 根据ID加载数据
        function loadDataById(id) {
            if (!id) return;
            
            // 模拟获取数据
            var mockData = {
                id: id,
                inboundCode: 'OS202406151234',
                inboundName: '测试外协入库单',
                inboundDate: '2024-06-15',
                workOrders: 'WO20240615001',
                documentStatus: 1,
                purchaseOrderNumber: 'PO20240615001',
                vendor: '北京精密零件厂',
                vendorCode: 'V002',
                warehouseId: 1,
                remark: '这是一个测试的外协入库单'
            };
            
            // 设置表单数据
            form.val('outsourcingAddForm', mockData);
            
            // 更新页面标题
            $('title').text('修改外协入库单');
            $('.layui-btn[lay-filter="outsourcingAddSubmit"]').text('更新');
        }
        
        // 页面加载时初始化
        loadWarehouses();
        loadVendors();
        
        // 获取ID参数并加载数据
        var id = getQueryString('id');
        if (id) {
            loadDataById(id);
        }
    });
</script> 